package com.myway.service.ibe.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.myway.dto.domain.FlightCriteria;
import com.myway.dto.domain.SegmentAndPassengerParam;
import com.myway.dto.ibe_param.DepatureDateParam;
import com.myway.dto.ibe_param.Destination;
import com.myway.dto.ibe_param.Origin;
import com.myway.dto.ibe_param.SegmentParam;
import com.myway.service.ibe.command.SearchOneCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by yufei on 2018/7/22.
 */
@Controller
@RequestMapping("/searchone")
public class SearchOneController{


    @Autowired
    private SearchOneCommand searchOneCommand;

    /**
     * 单程舱等价格
     * @param param
     * @return
     */
    @RequestMapping("/ow")
    @ResponseBody
    public String getOwPrice(@RequestBody String param){
        SegmentAndPassengerParam spp = JSON.parseObject(param, SegmentAndPassengerParam.class);
        String response = null;
        try {
            response = searchOneCommand.execute(spp,true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * search单程，往返程查询
     * @param param
     * @return
     */
    @RequestMapping("/price")
    @ResponseBody
    public String getPrice(@RequestBody String param) throws Exception{
        /*System.out.println("---getPrice---"+param);*/
        FlightCriteria criteria = JSON.parseObject(param,FlightCriteria.class);
        //FlightCriteriaParam criteria = JSON.parseObject(param,FlightCriteriaParam.class);
        String travlType = criteria.getTravlType();
        List<SegmentParam> segments = Lists.newArrayList();
        //航段1（去程）
        SegmentParam segmentParamOne = new SegmentParam();
        //设置起飞时间
        String strDate = criteria.getFlightDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date depDate = sdf.parse(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(depDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        System.out.print("---getPrice---"+month);
        int day = calendar.get(Calendar.DATE);
        DepatureDateParam depatureDateParam = new DepatureDateParam();
        depatureDateParam.setYear(year);
        depatureDateParam.setMonth(month);
        depatureDateParam.setDay(day);
        segmentParamOne.setDepartureDate(depatureDateParam);
        //设置出发地
        List<Origin> origins = Lists.newArrayList();
        Origin origin = new Origin();
        origin.setName(criteria.getOrgCity());
        origins.add(origin);
        segmentParamOne.setOrigin(origins);
        //设置目的地
        List<Destination> destinations = Lists.newArrayList();
        Destination destination = new Destination();
        destination.setName(criteria.getDstCity());
        destinations.add(destination);
        segmentParamOne.setDestination(destinations);
        segments.add(segmentParamOne);
        if(travlType.equals("rt")){
            //返程
            SegmentParam segmentParamTwo = new SegmentParam();
            //设置返程起飞时间
            String strArrDate = criteria.getArrDate();
            Date arrDate = sdf.parse(strArrDate);
            calendar.setTime(arrDate);
            int yearArr = calendar.get(Calendar.YEAR);
            int monthArr = calendar.get(Calendar.MONTH)+1;
            System.out.print("---getPrice---"+monthArr);
            int dayArr = calendar.get(Calendar.DATE);
            DepatureDateParam depatureDateParamTwo = new DepatureDateParam();
            depatureDateParamTwo.setYear(yearArr);
            depatureDateParamTwo.setMonth(monthArr);
            depatureDateParamTwo.setDay(dayArr);
            segmentParamTwo.setDepartureDate(depatureDateParamTwo);
            //设置出发地
            List<Origin> originsArr = Lists.newArrayList();
            Origin originArr = new Origin();
            originArr.setName(criteria.getDstCity());
            originsArr.add(origin);
            segmentParamOne.setOrigin(originsArr);
            //设置目的地
            List<Destination> destinationsArr = Lists.newArrayList();
            Destination destinationArr = new Destination();
            destinationArr.setName(criteria.getOrgCity());
            destinationsArr.add(destinationArr);
            segmentParamOne.setDestination(destinationsArr);
            segments.add(segmentParamTwo);
        }
        String response = null;
        try {
//            response = searchOneCommand.execute(segments,true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    /**
     * 日历价格
     * @param param
     * @return
     */
    @RequestMapping("/calendar")
    @ResponseBody
    public String getCalendarPrice(@RequestBody String param){
        SegmentAndPassengerParam spp = JSON.parseObject(param, SegmentAndPassengerParam.class);
        String response = null;
        try {
            response = searchOneCommand.execute(spp,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 日历价格new接口
     */
    @RequestMapping("/calendarprice")
    @ResponseBody
    public String getCalendarPriceNew(@RequestBody String param){


        return "";
    }
}
